﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        static Socket client;
        static IPEndPoint iep;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Byte[] data = new Byte[1024];
            data = Encoding.Unicode.GetBytes(txtNhap.Text);
            client.Send(data);
            txtHienThi.Text += "\n";
            txtHienThi.Text += txtNhap.Text;
            txtNhap.Text = "";
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            Thread a = new Thread(ketnoi);
            a.Start();
        }
        void ketnoi()
        {
            iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 100);
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            client.Connect(iep);
            while (true)
            {
                byte[] nhan = new byte[1024];
                client.Receive(nhan);
                txtHienThi.Text += "\n";
                txtHienThi.Text += Encoding.Unicode.GetString(nhan);
                
            }
        }

        
    }
}
